package leetcode;

/**
 * @Classname MaxProfit_0122
 * @Author happytaohaha
 * @Date 2020/3/24
 * @Description 买卖股票的最佳时机 II
 */
public class MaxProfit_0122 {
    public static int maxProfit(int[] prices) {
        int len = prices.length;
        if(len < 2) {return 0;}
        int min = prices[0];
        int max = prices[0];
        int profit = 0;
        for(int i = 0; i < len-1 ; i++){
            if(prices[i+1] >= prices[i]){
                max = prices[i+1];
            }
            //如果i+1 小于 i 时 或者i 为倒数第二个数 < i+1 时 执行下面的式子
            if(prices[i+1] < prices[i] ||i == len -2){
                if(max -min > 0){
                    profit += max - min;
                }
                min = prices[i+1];
                max = prices[i+1];
            }
        }
        return profit;
    }

    public static void main(String[] args) {
        System.out.println(maxProfit(new int[]{1,2,3,4,5}));
    }
}
